import '@/libs/jquery-vender.js'
import layer from 'layui-layer'

let utils = {

}

utils.title = function (title) {
  title = title || 'iView admin'
  window.document.title = title
}

utils.success = function (data) {
  return data && data.code && data.code === '000000'
}

utils.msg = function (msg) {
  layer.msg(msg)
}

utils.alert = function (msg, callback, cancelHandler) {

}

var loading
utils.load = function () {
  loading = layer.load(1)
}

utils.closeLoad = function () {
  layer.close(loading)
}

// 设置cookie
utils.setCookie = function (cname, cvalue, minutes) {
  var d = new Date()
  d.setTime(d.getTime() + (minutes * 60 * 1000))
  var expires = 'expires=' + d.toUTCString()
  console.info(cname + '=' + cvalue + '; ' + expires)
  document.cookie = cname + '=' + cvalue + '; ' + expires
  console.info(document.cookie)
}

// 获取cookie
utils.getCookie = function (cname) {
  var name = cname + '='
  var ca = document.cookie.split(';')
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i]
    while (c.charAt(0) === ' ') c = c.substring(1)
    if (c.indexOf(name) !== -1) return c.substring(name.length, c.length)
  }
  return ''
}

// 清除cookie
utils.clearCookie = function (cname) {
  this.setCookie(cname, '', -1)
}

// 判断用户是否已登录
utils.isLogin = function () {
  return this.getCookie('Authorization') !== ''
}

export default utils
